package com.lvyq.myplayer.util

import android.graphics.Rect
import android.view.View
import androidx.recyclerview.widget.RecyclerView

/**
 * 文件描述：GridSpacingItemDecoration  通过 ItemDecoration 设置子项间距
 * 作者：lvyq
 * 创建时间：2025-03-26
 * 修改时间：2025-03-26
 */
class GridSpacingItemDecoration(
    private val spanCount: Int,
    private val spacing: Int,
    private val includeEdge: Boolean = true
) : RecyclerView.ItemDecoration() {

    override fun getItemOffsets(
        outRect: Rect,
        view: View,
        parent: RecyclerView,
        state: RecyclerView.State
    ) {
        val position = parent.getChildAdapterPosition(view)
        val column = position % spanCount // 当前列索引

        val halfSpacing = spacing / 2f // 将间距拆分为左右两半
        val adjustedSpacing = if (includeEdge) spacing else spacing / 2 // 边缘调整

     /*   // 计算水平间距
        outRect.left = (column * spacing / spanCount).toInt()
        outRect.right = spacing - outRect.left

        // 计算垂直间距（首行特殊处理）
        outRect.top = if (position < spanCount) adjustedSpacing else halfSpacing.toInt()
        outRect.bottom = halfSpacing.toInt()*/

        //统一固定所有行列的上下左右边距
        outRect.left=halfSpacing.toInt()
        outRect.right=halfSpacing.toInt()
        outRect.top=halfSpacing.toInt()
        outRect.bottom=halfSpacing.toInt()
    }
}